﻿using System;
using System.Windows.Data;
using CannedHeater.ViewModel;

namespace CannedHeater.View.Converters
{
    /// <summary>
    /// Converts between <see cref="ModeType"/> (see ControlView), and boolean value (control enabled or not)
    /// Quite specific, see ControlViewModel for usage
    /// </summary>
    public class ControlModeConverter : IValueConverter
    {
        /// <summary>
        /// Converts <see cref="ModeType"/> to boolean, depending on parameter
        /// </summary>
        /// <param name="value">ModeType</param>
        /// <param name="targetType">Unused</param>
        /// <param name="parameter">String representation of ModeType (Auto, Manual, Time, Temp)</param>
        /// <param name="culture">Unused</param>
        /// <returns>
        /// True if ModeType in <paramref name="value"/> and <paramref name="parameter"/> are equal,
        /// or if <paramref name="parameter"/> is "Manual" and <paramref name="value"/> is not <see cref="ModeType.Auto"/>.
        /// Otherwise returns false.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || parameter == null) return false; // Just in case

            ModeType mode = (ModeType)value;
            ModeType param = StringToModeType((string)parameter);

            if (mode == param)
                return true;
            else if (param == ModeType.Manual && mode != ModeType.Auto)
                return true;
            else
                return false;

        }

        /// <summary>
        /// Converts a boolean into a <see cref="ModeType"/> depending on the parameter
        /// </summary>
        /// <param name="value">Boolean</param>
        /// <param name="targetType">Unused</param>
        /// <param name="parameter">String representation of ModeType (Auto, Manual, Time, Temp)</param>
        /// <param name="culture">Unused</param>
        /// <returns>
        /// If <paramref name="value"/> is false, returns <see cref="ModeType.Unknown"/>,
        /// if true, returns <see cref="ModeType"/> corresponding to <paramref name="parameter"/>.
        /// If either is null, returns default <see cref="ModeType.Auto"/>
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || parameter == null) return ModeType.Auto; // Just in case

            bool val = (bool)value;
            if (!val) return ModeType.Unknown;
            else {
                ModeType param = StringToModeType((string)parameter);
                return param;
            }
        }

        /// <summary>
        /// Converts string to its corresponding mode
        /// </summary>
        /// <param name="str">String representation of mode</param>
        /// <returns>ModeType for corresponding string</returns>
        private ModeType StringToModeType(string str)
        {
            switch ( str )
            {
                default:
                case "Auto":
                    return ModeType.Auto;

                case "Manual":
                    return ModeType.Manual;

                case "Time":
                    return ModeType.Time;

                case "Temp":
                    return ModeType.Temp;
            }
        }
    }
}
